//
//  ToastManager.m
//  CommonHUD
//
//  Created by iMac on 2018/10/16.
//  Copyright © 2018年 Jason. All rights reserved.
//

#import "ToastManager.h"
#import "AppDelegate.h"
static ToastManager *toastManager;
@implementation ToastManager

+ (ToastManager *) shardInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        toastManager = [[ToastManager alloc] init];
        
        [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[NSClassFromString(@"MBProgressHUD")]].color = [UIColor whiteColor];
    });
    return toastManager;
}

- (void)showSystemProgress:(UIView*)view{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
   // hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.5f];
}

- (void)showCustomProgress:(UIView*)view{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.backgroundColor = [UIColor whiteColor];
    UIImageView *gifImageView = [[UIImageView alloc] init];
    NSMutableArray * images = [NSMutableArray array];
    for (int index = 0; index< 0; index++) {
        NSString * imageName = [NSString stringWithFormat:@"loading%d.png",index];
        UIImage *image = [UIImage imageNamed:imageName];
        [images addObject:image];
    }
    gifImageView.animationImages = images;
    gifImageView.animationDuration = 1.65;
    [gifImageView startAnimating];
    hud.customView = gifImageView;
    hud.bezelView.backgroundColor = [UIColor whiteColor];
}

- (void)showToast:(NSString *)toastStr{
    UIWindow *keywindow = [ToastManager keyWindow];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:keywindow animated:YES];
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = NSLocalizedString(toastStr, @"HUD message title");
    hud.label.numberOfLines = 0;
    hud.label.textColor = [UIColor grayColor];
    hud.offset = CGPointMake(0.0f, 0.0f);
    [hud hideAnimated:YES afterDelay:1.5f];
}

- (void)showSuccessToastWithView:(UIView*)view toastString:(NSString *)toastString{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // Set the custom view mode to show any view.
    hud.mode = MBProgressHUDModeCustomView;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    // Set an image view with a checkmark.
    UIImage *image = [UIImage imageNamed:@"Success"];
    hud.customView = [[UIImageView alloc] initWithImage:image];
    // Looks a bit nicer if we make it square.
    hud.square = YES;
    // Optional label text.
    hud.label.text = NSLocalizedString(toastString, @"HUD done title");
    hud.label.textColor = [UIColor whiteColor];
    [hud hideAnimated:YES afterDelay:1.5f];
}

- (void)showErrorToastWithView:(UIView*)view toastString:(NSString *)toastString{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // Set the custom view mode to show any view.
    hud.mode = MBProgressHUDModeCustomView;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    // Set an image view with a checkmark.
    UIImage *image = [UIImage imageNamed:@"Error"];
    hud.customView = [[UIImageView alloc] initWithImage:image];
    // Looks a bit nicer if we make it square.
    hud.square = YES;
    // Optional label text.
    hud.label.text = NSLocalizedString(toastString, @"HUD done title");
    hud.label.textColor = [UIColor whiteColor];
    [hud hideAnimated:YES afterDelay:1.5f];
}

- (void)hideProgress:(UIView*)view{
    [MBProgressHUD hideHUDForView:view animated:YES];
}

#pragma mark -keyWindow
+ (UIWindow *)keyWindow {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIWindow *keywindow = delegate.window;
    return keywindow;
}
@end
